<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Orderaff extends CI_Controller {

	var $per_page;
	var $user_id;		

	public function __construct()
	{
		parent::__construct();

		$logged_in = $this->session->userdata('logged_in');
		if($logged_in)
		{
		   $session_data = $this->session->userdata('logged_in');
		   $data['username'] = $session_data['username'];		  
		   // set user id
		   $this->user_id = $session_data['id'];
		}
		else
		{
	     //If no session, redirect to login page
	     redirect('login', 'refresh');
   		}   	

		$this->template->set('nav', $this->uri->segment(1));
		$this->template->set('title', 'Cộng tác viên');
		$this->template->set('section', 'Đơn hàng');		

		////////////// load product model, order /////////////////////////
		$this->load->model('Product_model');
		$this->load->model('Order_model');
		$this->load->model('Order_sizes_model');
		$this->load->model('Storage_product_model');
		$this->load->model('Storage_model');
		$this->load->model('Code_export_form_model');
		$this->load->model('Product_sizes_model');
		$this->load->model('Storage_product_history_model');		
		$this->load->model('User_model');
		/////////////////////////////////////////////////////////////////

		$this->per_page = 5;
	}	

	public function index($start = false)
	{
		$data = array();
		$search = array();

		// add condition search with user_id
		$search['user_id'] = $this->user_id;

		$user = $this->User_model->getUser($search['user_id']);

		$data['view_order'] = $user->view_order;

		/////////////////////// TÌM KIẾM /////////////////////////////////////////////////////////////////////
		
		$order_status = $this->input->post('status');
		$fromDate = $this->input->post('fromDate');
		$toDate = $this->input->post('toDate');
		$phone = $this->input->post('phone');
		$customer = $this->input->post('customer');

		$data['order_status'] = $data['fromDate'] = $data['toDate'] = $data['phone'] = $data['customer'] = '';
		if(!empty($order_status)){
			$search['order_status'] = $order_status;
			$data['order_status'] = $order_status;
		}
		if(!empty($fromDate)){
			$search['from_date'] = $fromDate;
			$data['fromDate'] = $fromDate;
		}
		if(!empty($toDate)){
			$search['to_date'] = $toDate;
			$data['toDate'] = $toDate;
		}
		if(!empty($phone)){
			$search['customer_phone'] = $phone;
			$data['phone_number'] = $phone;
		}
		if(!empty($customer)){
			$search['customer_name'] = $customer;
			$data['customer_name'] = $customer;
		}

		if(empty($start))
			$start = 1;

		$start = ($start - 1) * $this->per_page;
		$limit = $this->per_page;

		//////////// search /////////////////

		$orders = $this->Order_model->loadAllV3($start, $limit, $search);	

		$data['orders'] = $orders['data'];
		$data['total'] = $orders['count'];		

		$tmp_orders = array();
		foreach ($data['orders'] as $row) 
		{
			$order_id = $row->id;
			$product_id = $row->product_id;

			$productWithSizes = $this->Product_model->listProductWithSize($order_id, $product_id);

			$xs = $s = $m = $l = $xl = $xxl = $xxxl = "";
			$xs_qty = $s_qty = $m_qty = $l_qty = $xl_qty = $xxl_qty = $xxxl_qty = '';
			$total_qty = "";
			$product_code = $product_title = $product_image = "";
			$product_price = $total_price = "";
			foreach ($productWithSizes as $p) 
			{
				$size = $p->size;
				$qty = $p->quantity;
				$product_code = $p->code;
				$product_title = $p->title;
				$product_image = $p->image;
				$product_price = $p->price;

				switch ($size) {
					case 'xs':
						$xs = $size;
						$xs_qty = $qty;
						$total_qty .= "Size XS: $xs_qty<br>";
						$total_price += $product_price * $xs_qty;
						break;
					case 's':
						$s = $size;
						$s_qty = $qty;
						$total_qty .= "Size S: $s_qty<br>";
						$total_price += $product_price * $s_qty;
						break;	
					case 'm':
						$m = $size;
						$m_qty = $qty;
						$total_qty .= "Size M: $m_qty<br>";
						$total_price += $product_price * $m_qty;
						break;
					case 'l':
						$l = $size;
						$l_qty = $qty;
						$total_qty .= "Size L: $l_qty<br>";
						$total_price += $product_price * $l_qty;
						break;
					case 'xl':
						$xl = $size;
						$xl_qty = $qty;
						$total_qty .= "Size XL: $xl_qty<br>";
						$total_price += $product_price * $xl_qty;
						break;
					case 'xxl':
						$xxl = $size;
						$xxl_qty = $qty;
						$total_qty .= "Size XXL: $xxl_qty<br>";
						$total_price += $product_price * $xxl_qty;
						break;	
					case 'xxxl':
						$xxxl = $size;
						$xxxl_qty = $qty;
						$total_qty .= "Size XXXL: $xxxl_qty<br>";
						$total_price += $product_price * $xxxl_qty;
						break;		
					default:						
						break;
				}				
			}
			$tmp_orders[] = array(
					'code'			=>	$product_code,	
					'order_id'		=>	$order_id,
					'product_title'	=>	$product_title,
					'product_image'	=>	$product_image,
					'product_price'	=>	$total_price,
					'customer_name'	=>	$data['view_order'] ? $row->customer_name : '********',
					'customer_phone'=>	$data['view_order'] ? $row->customer_phone : '********',
					'address'		=>	$data['view_order'] ? $row->shipping_address : '********',
					'quantity'		=>	$total_qty,
					'status'		=>	$row->status,
					'coupon'		=>	$row->coupon,
					'created_at'	=>	$row->created_at,
				);
		}		

		$data['orders'] = $tmp_orders;

		$this->template->load_main('admin/template','admin/orderaff',$data);
	}

	public function search()
	{
		$this->template->load_main('admin/template','admin/orderaff');
	}

}		